package com.example.kliven.because.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.kliven.because.R;
import com.example.kliven.because.adapter.RankingAdapter;
import com.example.kliven.because.bean.RankingBean;
import com.example.kliven.because.intentbean.NetRankingBean;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class RankingActivity extends Activity {

    private RecyclerView recyclerView;
    private RankingAdapter rankingAdapter;
    private RequestQueue queue;
    private List<RankingBean> rankingBeans;
    private String url = "http://yinweiapi.paidui.com:81/appIndex/getToptenzShare.do?memberId=289167&start=0&pageSize=15&cityId=0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranking);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new GridLayoutManager(this,2));

        getNetInfo();
    }

    private void getNetInfo() {
        queue = Volley.newRequestQueue(this);
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                rankingBeans = new ArrayList<>();
                Gson gson = new Gson();
                NetRankingBean netRankingBean = gson.fromJson(response,NetRankingBean.class);
                for (int i = 0; i < netRankingBean.getData().getApp_discover_toptenz().size(); i++) {
                    RankingBean rankingBean = new RankingBean();
                    String member_name = netRankingBean.getData().getApp_discover_toptenz().get(i).getMember_name();
                    rankingBean.setMember_name(member_name);
                    rankingBean.setShop_name(netRankingBean.getData().getApp_discover_toptenz().get(i).getShop_name());
                    rankingBean.setDish_name(netRankingBean.getData().getApp_discover_toptenz().get(i).getDish_name());
                    rankingBean.setTime_tips(netRankingBean.getData().getApp_discover_toptenz().get(i).getTime_tips());
                    rankingBean.setAward_count(netRankingBean.getData().getApp_discover_toptenz().get(i).getAward_count());
                    rankingBean.setIco(netRankingBean.getData().getApp_discover_toptenz().get(i).getIco());
                    rankingBean.setMain_photo_url(netRankingBean.getData().getApp_discover_toptenz().get(i).getMain_photo_url());
                    Log.d("RankingActivity", netRankingBean.getData().getApp_discover_toptenz().get(i).getMain_photo_url());
                    rankingBeans.add(rankingBean);
                }
                rankingAdapter = new RankingAdapter(RankingActivity.this,rankingBeans);
                recyclerView.setAdapter(rankingAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(request);
    }
}
